package club.calong.sanniu.service;

import club.calong.sanniu.http.response.ListResponseData;
import club.calong.sanniu.pojo.Plate;
import club.calong.sanniu.pojo.Role;
import club.calong.sanniu.repository.RoleRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService {


	private final RoleRepository repository;

	public RoleService(RoleRepository repository) {
		this.repository = repository;
	}

	public boolean insert(Role role) {
		return repository.insert(role) == 1;
	}

	public boolean delete(Integer id) {

		return repository.deleteById(id) == 1;
	}

	public boolean update(Role role) {

		return repository.updateById(role) == 1;
	}

	public Role select(Integer id) {

		return repository.selectById(id);
	}

    public ListResponseData<Role> list(Integer page, Integer size) {

		if (page != null && size != null) {
			QueryWrapper<Role> wrapper = new QueryWrapper<>();
			wrapper.orderByAsc(Role.ROLE_NAME);
			Page<Role> rolePage = repository.selectPage(new Page<>(page, size), wrapper);
			return new ListResponseData<>(rolePage.getRecords(), rolePage.getTotal());
		} else {
			List<Role> roles = repository.selectList(null);
			return new ListResponseData<>(roles, (long) roles.size());
		}
	}
}
